//
//  RWDeliveryViewController.m
//  RedWine
//
//  Created by Liufangfang on 2017/3/26.
//  Copyright © 2017年 HuiBangKe. All rights reserved.
//

#import "RWDeliveryViewController.h"

#define kDeliveryButtonBaseTag 700

@interface RWDeliveryViewController ()

@property (nonatomic, strong) NSArray *deliveryStyles;
@property (nonatomic, strong) NSString *currentDeliveryStyle;

@property (nonatomic, strong) UIButton *allDeliveryBtn;
@property (nonatomic, strong) UIButton *workDayDeliveryBtn;
@property (nonatomic, strong) UIButton *weekEndDeliveryBtn;

@end

@implementation RWDeliveryViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    [self setNavigation];
    [self setDeliveryUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Data

- (void)initData{

    _deliveryStyles = @[@"送货时间不限", @"仅周一至周五送货", @"仅双休日／节假日送货"];
}

#pragma mark - Config UI

- (void)setNavigation{
    
    self.title = @"收货方式";
}

- (void)setDeliveryUI{

    _deliveryTV = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_deliveryTV];
    _deliveryTV.backgroundColor = [FTYColor lightGrayColor];
    _deliveryTV.dataSource = self;
    _deliveryTV.delegate = self;
    _deliveryTV.tableHeaderView = [self setHeaderView];
    _deliveryTV.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)setHeaderView{

    UIView *headerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, 90.*FTY_HEIGHT_BASE)];
    headerV.backgroundColor = [UIColor clearColor];
    
    UILabel *titleLab = [[UILabel alloc] init];
    [headerV addSubview:titleLab];
    titleLab.text = @"快递公司会尽快按您选择的送货时间配送，如遇特殊情况（天气、环境等）无法按照您的需求配送，还请您谅解。";
    titleLab.numberOfLines = 0;
    titleLab.textColor = [FTYColor blackColor];
    titleLab.font = FTY_FONTTSIZE(15);
    [titleLab sizeToFit];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_offset(0);
        make.left.mas_offset(10*FTY_WIDTH_BASE);
        make.right.mas_offset(-10*FTY_WIDTH_BASE);
    }];
    
    UIView *line = [[UIView alloc] init];
    [headerV addSubview:line];
    line.backgroundColor = [FTYColor whiteColor];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(CGRectGetHeight(headerV.frame)-8);
        make.left.right.mas_offset(0);
        make.height.mas_offset(.1);
    }];
    
    return headerV;
}
#pragma mark - Notificaiton Method

#pragma mark - Notificaiton Invoke Methods

#pragma mark - <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50.*FTY_HEIGHT_BASE;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * const deliveryID = @"deliveryID";
    UITableViewCell *deliveryCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:deliveryID];
    deliveryCell.selectionStyle = UITableViewCellSelectionStyleNone;
    deliveryCell.backgroundColor = [FTYColor clearColor];
    
    NSInteger row = indexPath.row;
    
    deliveryCell.textLabel.text = _deliveryStyles[row];
    deliveryCell.textLabel.textColor = [FTYColor blackColor];
    deliveryCell.textLabel.font = FTY_FONTTSIZE(15);
    
    //画圆
    CGFloat cycle_WH = 15;
    UIButton *cycleBtn = [[UIButton alloc] init];
    [deliveryCell addSubview:cycleBtn];
    cycleBtn.frame = CGRectMake(0, 0, cycle_WH, cycle_WH);
    cycleBtn.layer.masksToBounds = YES;
    cycleBtn.layer.cornerRadius = cycle_WH/2;
    cycleBtn.layer.borderColor = [FTYColor whiteColor].CGColor;
    cycleBtn.layer.borderWidth = 1;
    [cycleBtn setImage:[UIImage imageNamed:@"noselected_icon"] forState:UIControlStateNormal];
    [cycleBtn setImage:[UIImage imageNamed:@"selected_icon"] forState:UIControlStateSelected];
    cycleBtn.tag = kDeliveryButtonBaseTag + row;
    [cycleBtn addTarget:self action:@selector(deliveryButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cycleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(deliveryCell.mas_centerY);
        make.right.mas_equalTo(-12.*FTY_WIDTH_BASE);
        make.height.width.mas_offset(cycle_WH);
    }];
    if (0 == row) {
        _allDeliveryBtn = cycleBtn;
    } else if(1 == row) {
        _workDayDeliveryBtn = cycleBtn;
    } else {
        _weekEndDeliveryBtn = cycleBtn;
    }
    
    return deliveryCell;
}

#pragma mark - <UITableViewDelegate>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
}

#pragma mark - Private Invoke Methods

- (void)deliveryButtonAction:(UIButton *)btn{

    btn.selected = !btn.selected;
    NSUInteger tag = btn.tag;
    
    if (700 == tag) {
        
        _deliveryStyle = RWAllDeliveryStyle;
        _workDayDeliveryBtn.selected = NO;
        _weekEndDeliveryBtn.selected = NO;
    } else if(701 == tag){
        
        _deliveryStyle = RWWorkDayDeliveryStyle;
        _allDeliveryBtn.selected = NO;
        _weekEndDeliveryBtn.selected = NO;
    } else if(702 == tag){
        
        _deliveryStyle = RWWeekEndDeliveryStyle;
        _allDeliveryBtn.selected = NO;
        _workDayDeliveryBtn.selected = NO;
    }
    
    _currentDeliveryStyle = _deliveryStyles[tag-700];
    
    if ([_delegate respondsToSelector:@selector(selectDeliveryViewController:didSelectDeliveryStyle:)]) {
        [_delegate selectDeliveryViewController:self didSelectDeliveryStyle:_currentDeliveryStyle];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
